package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.fragments.NoticeDoFragment;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-07-09.
 * Description:提醒主页（用户执行单，提醒单）
 */
public class NoticeActivity extends BaseFragmentActivity implements View.OnClickListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private ImageView iv_add;

    private LinearLayout ll_do;
    private TextView tv_do;
    private View view_do;
    private LinearLayout ll_remind;
    private TextView tv_remind;
    private View view_remind;

    private ViewPager viewPager;

    private int type = 0;//0提醒单1执行单

    private NoticeDoFragment remindFragment;
    private NoticeDoFragment doFragment;
    private List<Fragment> fragments;
    private FragmentManager fragmentManager;

    private PopupWindow popup_add;//右上角操作框
    private LinearLayout ll_dolist;
    private LinearLayout ll_remindlist;
    private LinearLayout ll_scalelist;
    private LinearLayout ll_questionlist;

    private String patientId;

    private boolean isFuzhen = false;

    @Override
    protected void updateByHandler(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);

        patientId = getIntent().getStringExtra("patientId");
        if (getIntent().hasExtra("isFuzhen")) {
            isFuzhen = getIntent().getBooleanExtra("isFuzhen", false);
        }

        initView();
        initPopup();
        if (isFuzhen) {
            if (remindFragment != null) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        remindFragment.expandFirstGroup();
                    }
                }, 2000);
            }
        }
    }

    private void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        iv_add = (ImageView) findViewById(R.id.iv_title_func);

        ll_do = (LinearLayout) findViewById(R.id.ll_notice_do);
        tv_do = (TextView) findViewById(R.id.tv_notice_do);
        view_do = findViewById(R.id.view_notice_do);
        ll_remind = (LinearLayout) findViewById(R.id.ll_notice_remind);
        tv_remind = (TextView) findViewById(R.id.tv_notice_remind);
        view_remind = findViewById(R.id.view_notice_remind);

        viewPager = (ViewPager) findViewById(R.id.viewpager_notice);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.notice));
        iv_add.setVisibility(View.VISIBLE);
        iv_add.setImageResource(R.drawable.nav_add);
        iv_back.setOnClickListener(this);
        iv_add.setOnClickListener(this);

        ll_do.setOnClickListener(this);
        ll_remind.setOnClickListener(this);

        doFragment = new NoticeDoFragment(0, patientId);
        remindFragment = new NoticeDoFragment(1, patientId);
        fragments = new ArrayList<>();
        fragments.add(remindFragment);
        fragments.add(doFragment);

        fragmentManager = this.getSupportFragmentManager();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tv_remind.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    view_remind.setBackgroundResource(R.color.light_blue);
                    tv_do.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    view_do.setBackgroundResource(R.color.white);
                    type = 0;
                } else {
                    tv_do.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    view_do.setBackgroundResource(R.color.light_blue);
                    tv_remind.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    view_remind.setBackgroundResource(R.color.white);
                    type = 1;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
                ((ViewPager) container).removeView(fragments.get(position).getView());
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                Fragment fragment = fragments.get(position);
                if (!fragment.isAdded()) { // 如果fragment还没有added
                    FragmentTransaction ft = fragmentManager.beginTransaction();
                    ft.add(fragment, fragment.getClass().getSimpleName());
                    ft.commit();
                    /**
                     * 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后
                     * 会在进程的主线程中,用异步的方式来执行。
                     * 如果想要立即执行这个等待中的操作,就要调用这个方法(只能在主线程中调用)。
                     * 要注意的是,所有的回调和相关的行为都会在这个调用中被执行完成,因此要仔细确认这个方法的调用位置。
                     */
                    fragmentManager.executePendingTransactions();
                }
                if (fragment.getView().getParent() == null) {
                    container.addView(fragment.getView()); // 为viewpager增加布局
                }
                return fragment.getView();
            }
        });
    }

    private void initPopup() {
        popup_add = new PopupWindow(this);
        View viewAdd = LayoutInflater.from(mContext).inflate(R.layout.popup_notice_add, null);
        popup_add.setContentView(viewAdd);

        popup_add.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_add.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_add.setFocusable(true);
        popup_add.setTouchable(true);
        popup_add.setOutsideTouchable(true);
        popup_add.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_add.setAnimationStyle(R.style.popupwindow_anim_alpha);

        ll_dolist = (LinearLayout) viewAdd.findViewById(R.id.ll_noticeadd_dolist);
        ll_remindlist = (LinearLayout) viewAdd.findViewById(R.id.ll_noticeadd_remindlist);
        ll_scalelist = (LinearLayout) viewAdd.findViewById(R.id.ll_noticeadd_scalelist);
        ll_questionlist = (LinearLayout) viewAdd.findViewById(R.id.ll_noticeadd_questionlist);

        ll_dolist.setOnClickListener(this);
        ll_remindlist.setOnClickListener(this);
        ll_scalelist.setOnClickListener(this);
        ll_questionlist.setOnClickListener(this);

        popup_add.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(NoticeActivity.this);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.iv_title_func:
                popup_add.showAsDropDown(iv_add, 0, ImageUtil.dip2px(10, mContext));
                BrightnessUtils.lightOff(NoticeActivity.this);
                break;
            case R.id.ll_notice_remind:
                if (type != 0) {
                    viewPager.setCurrentItem(0);
                }
                break;
            case R.id.ll_notice_do:
                if (type != 1) {
                    viewPager.setCurrentItem(1);
                }
                break;
            case R.id.ll_noticeadd_remindlist:
                popup_add.dismiss();
//                Intent remindIntent = new Intent(mContext, PerformListActivity.class);
//                remindIntent.putExtra("type", 1);
//                remindIntent.putExtra("patientId", patientId);
//                remindIntent.putExtra("usefinish", true);
//                startActivity(remindIntent);
                Intent iDolist = new Intent(mContext, NewDolistActivity.class);
                iDolist.putExtra("type", 1);
                iDolist.putExtra("patientId", patientId);
                startActivity(iDolist);
                break;
            case R.id.ll_noticeadd_dolist:
                popup_add.dismiss();
                Intent performIntent = new Intent(mContext, PerformListActivity.class);
                performIntent.putExtra("type", 0);
                performIntent.putExtra("patientId", patientId);
                performIntent.putExtra("usefinish", true);
                startActivity(performIntent);

//                Intent iBetterPlan = new Intent(mContext, BetterPlanEditActivity.class);
//                iBetterPlan.putExtra("patientId", patientId);
//                startActivity(iBetterPlan);
                break;
            case R.id.ll_noticeadd_scalelist:
                //跳入量表列表，然后给当前患者将要选择的量表
                popup_add.dismiss();
                Intent scaleIntent = new Intent(mContext, ScaleQuestionActivity.class);
                scaleIntent.putExtra("patientId", patientId);
                scaleIntent.putExtra("usefinish", true);
                startActivity(scaleIntent);
                break;
            case R.id.ll_noticeadd_questionlist:
                //跳入调查问卷列表，然后给当前患者将要选择的调查问卷
                popup_add.dismiss();
                Intent questionIntent = new Intent(mContext, QuestionListActivity.class);
                questionIntent.putExtra("patientId", patientId);
                startActivity(questionIntent);
                break;
        }
    }
}
